package kr.co.bettersoft.sendme.provider;

import java.util.ArrayList;

import kr.co.bettersoft.sendme.activity.SendMeTempMessageListActivity;
import kr.co.bettersoft.sendme.util.SendMeCommon;
import kr.co.bettersoft.sendme.activity.R;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class SendMeTempMessageAdapter extends BaseAdapter {
	
	private Context context;
	private ArrayList<SendMeVoice> tempMessageList;
	
	private SendMeVoice tempMessage;
	LayoutInflater layoutInflater;
	
	private SendMeTempMessageListActivity receiveSendMeTempMessageListActivity;
	
	SendMeCommon sendMeCommon;
	
	String TAG = "SendMeTempMessageAdapter";
	
	View view;
	
	ImageButton tempMessagePlayBtn;		
	ImageButton tempMessageSendBtn;
	ImageButton menuDeleteBtn;
	TextView txtViewFriendName;				// 친구 이름.
	
	public SendMeTempMessageAdapter(Context context, SendMeTempMessageListActivity sendMeTempMessageListActivity, ArrayList<SendMeVoice> tempMessageList) {
		this.context = context;
		this.tempMessageList = tempMessageList;
		this.receiveSendMeTempMessageListActivity = sendMeTempMessageListActivity;
		
		layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		sendMeCommon = new SendMeCommon();
	}
	
	@Override
	public int getCount() {
		if(tempMessageList!=null){
			return tempMessageList.size();
		}else{
			return 0;
		}
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return tempMessageList.get(position);
	}


	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		view = convertView;
		
		ViewWrapper wrapper = null;
		// 태그를 이용하여 position 값을 정확한 값을 사용하도록 함. 태그를 저장후 나중에 태그값을 꺼내어 사용
		if(view == null) {
			view = layoutInflater.inflate(R.layout.activity_send_me_temp_message_list_adapter, parent, false);
			wrapper = new ViewWrapper(view);
			view.setTag(wrapper);
		} else {
			wrapper = (ViewWrapper)view.getTag();
		}
		
		tempMessage = tempMessageList.get(position);
		
		
		tempMessagePlayBtn = (ImageButton) view.findViewById(R.id.recordingMenuPlayBtn);
		tempMessageSendBtn = (ImageButton) view.findViewById(R.id.recordingMenuSendBtn);
		menuDeleteBtn = (ImageButton) view.findViewById(R.id.menuDeleteBtn);					// 삭제 버튼	// 밀어서 지우기 용도
		txtViewFriendName = (TextView)view.findViewById(R.id.txtViewFriendName);
		
		tempMessagePlayBtn.setTag(position);
		tempMessageSendBtn.setTag(position);
		menuDeleteBtn.setTag(position);
		txtViewFriendName.setTag(position);
		
		
		tempMessagePlayBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int selectedPosition = Integer.parseInt(v.getTag()+"");
//				receiveSendMeTempMessageListActivity.playAudioMessage(selectedPosition);
				receiveSendMeTempMessageListActivity.playAudioMessage(tempMessageList.get(selectedPosition));
			}
		});
		tempMessageSendBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int selectedPosition = Integer.parseInt(v.getTag()+"");
				receiveSendMeTempMessageListActivity.sendAudioMessage(tempMessageList.get(selectedPosition));
			}
		});
		menuDeleteBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int selectedPosition = Integer.parseInt(v.getTag()+"");
				receiveSendMeTempMessageListActivity.confirmDelete(tempMessageList.get(selectedPosition));		 
			}
		});
		
		
		// 친구 이름을 세팅 한다.
		if(Integer.parseInt(tempMessage.getReceiverCount())>1){		// 그룹
			txtViewFriendName.setText("그룹음성메시지");
		}else{
			if(tempMessage.getReceiverName().length()>0){
				txtViewFriendName.setText(tempMessage.getReceiverName());
			}else{
				txtViewFriendName.setText(tempMessage.getReceiver());
			}
		}
		
		if(tempMessage.getReceiverCount().equals("1")){
			// 친구 전화번호 세팅 한다.	  
			wrapper.getTxtViewFriendPhoneNumber().setText(sendMeCommon.phoneNumberForShowFormat(tempMessage.getReceiver()));
		}
		
		// 텍스트뷰 터치 
		txtViewFriendName.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int selectedPosition = Integer.parseInt(v.getTag()+"");
				
				if(Integer.parseInt(tempMessageList.get(selectedPosition).getReceiverCount())>1){	// 여러명일때 동작
					
					receiveSendMeTempMessageListActivity.groupEditMode = true;
					receiveSendMeTempMessageListActivity.processTempMessageGroupEditList(tempMessageList.get(selectedPosition).getSqliteKey(), selectedPosition); 
				}
			}
		});
		
		
		// 삭제 버튼 보일지 여부
		if(tempMessage.isShowDeleteBtnYn()){
			wrapper.getLayoutButtons1().setVisibility(View.GONE);
			wrapper.getLayoutButtons2().setVisibility(View.VISIBLE);
		}else{
			wrapper.getLayoutButtons1().setVisibility(View.VISIBLE);
			wrapper.getLayoutButtons2().setVisibility(View.GONE);
		}
		
		// 메시지 등록일 표시
		wrapper.getRegisterDate().setText(tempMessage.getRegisterDate());
		
		return view;
	}
	
	class ViewWrapper {
		View base;
		
//		TextView txtViewFriendName;				// 친구 이름.
		TextView txtViewFriendPhoneNumber;				// 친구 전화번호.
		TextView registerDate;					// 메시지 등록 일
		
		
		View layoutButtons1;			// 기본 버튼 레이아웃
		View layoutButtons2;			// 삭제 버튼 레이아웃
		
		ViewWrapper(View base) {
			this.base = base;
		}
		
		TextView getTxtViewFriendPhoneNumber() {
			if(txtViewFriendPhoneNumber == null) {
				txtViewFriendPhoneNumber = (TextView)view.findViewById(R.id.txtViewFriendPhoneNumber);
			}
			
			return txtViewFriendPhoneNumber;
		}
		
		TextView getRegisterDate() {
			if(registerDate == null) {
				registerDate = (TextView)view.findViewById(R.id.registerDate);
			}
			return registerDate;
		}
		
		View getLayoutButtons1() {
			if(layoutButtons1 == null) {
				layoutButtons1 = (View)view.findViewById(R.id.layout_buttons1);
			}
			return layoutButtons1;
		}
		
		View getLayoutButtons2() {
			if(layoutButtons2 == null) {
				layoutButtons2 = (View)view.findViewById(R.id.layout_buttons2);
			}
			return layoutButtons2;
		}
	}

}